package com.example.orderfood.db;
/**
 * 数据库
 */

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import com.example.orderfood.R;
import com.example.orderfood.dao.UserDao;
import com.example.orderfood.tools.Tools;
import java.io.ByteArrayOutputStream;
public class DBUtil extends SQLiteOpenHelper {
    private static final  String DB_NAME="db_takeaway.db";//数据库名字
    public static SQLiteDatabase db=null;//通过db进行操作数据库
    private static  final int VERSION=47;
    private  Context context;
    public DBUtil(Context context){
        super(context,DB_NAME,null,VERSION,null);
        this.context=context;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("PRAGMA foreign_keys = false");
        byte tx[]=getImg(R.drawable.upimg);//将二进制转换成图品
        String uuid=Tools.getUUID();
        String filePath=Tools.getImagePath()+"/"+uuid+"tx.png";
        Tools.saveByteArrayAsPng(tx,filePath,context);
        db.execSQL("drop table if exists d_business");//商家ID,密码，店名，描述，类型
        db.execSQL("create table d_business(s_id varchar(20) primary key," +
                "s_pwd varchar(20)," +
                "s_name varchar(20)," +
                "s_describe varchar(200)," +
                "s_type varchar(20)," +
                "s_img varchar(255))");
        UserDao.addBusiness(db,"root","123456","喜喜的店子","卖一些小玩具...","火锅店.",filePath);
        db.execSQL("drop table if exists d_user");//商家ID,密码，店名，描述，类型
        db.execSQL("create table d_user(s_id varchar(20) primary key," +
                "s_pwd varchar(20)," +
                "s_name varchar(20)," +
                "s_sex varchar(200)," +
                "s_address varchar(20)," +
                "s_phone varchar(20)," +
                "s_img varchar(255))");

        UserDao.addUser(db,"admin","123456","小铭用户","男","北京市","123123",filePath);
        //存储商品表
        //分为商家表和用户表，第一步先创建一个商家表
        db.execSQL("drop table if exists d_food");
        //商家ID,密码，店名，描述，类型
        db.execSQL("create table d_food(s_food_id varchar(20)primary key ," +
                "s_business_id varchar(20) ," +//店铺名
                "s_food_name varchar(20)," +
                "s_food_des varchar(200)," +
                "s_food_price varchar(20)," +
                "s_food_img varchar(255))");//头像
        String mltPath=Tools.getImagePath()+"/"+uuid+"yff_5.jpg";
        Tools.saveByteArrayAsPng(getImg(R.drawable.mlt),mltPath,context);

        String da[]={"1","root","杨枝甘露","属于你的唯一奶茶.","12.8", mltPath};
      db.execSQL("insert into d_food (s_food_id,s_business_id,s_food_name,s_food_des,s_food_price,s_food_img) values(?,?,?,?,?,?)",da);
        String klmPath=Tools.getImagePath()+"/"+uuid+"yff_6.jpg";
        Tools.saveByteArrayAsPng(getImg(R.drawable.yff_6),klmPath,context);
        String da1[]={"2","root","棒打鲜橙","你中意的果茶","19.86", klmPath};
        db.execSQL("insert into d_food (s_food_id,s_business_id,s_food_name,s_food_des,s_food_price,s_food_img) values(?,?,?,?,?,?)",da1);
        //订单ID，订单时间，购买人，商家，商品ID
        //商品ID，商家用户，名字，描述，单价，数量
        db.execSQL("drop table if exists d_orders");//代表的是用户表
        db.execSQL("create table d_orders(s_order_id varchar(20)primary key ," +//订单ID
                "s_order_time varchar(20) ," +
                "s_order_user_id varchar(20)," +
                "s_order_user_name varchar(20)," +
                "s_order_user_img varchar(255)," +
                "s_order_business varchar(200)," +
                "s_order_business_name varchar(200)," +
                "s_order_business_img varchar(255)," +
                "s_order_business_id varchar(20)," +
                "s_order_sta varchar(20)," +
                "s_order_adress varchar(255))");
        String img[]={filePath,filePath};
            // select * from d_orders
        db.execSQL("insert into d_orders values('1','2023-10-07','admin','小杨',?,'root','喜喜的店子',?,'1','1','小杨-四川绵阳，西南你财经大学天府学院-123123')",img);
        db.execSQL("insert into d_orders values('2','2023-12-07','admin','小杨',?,'root','喜喜的店子',?,'2','1','小明-四川绵阳，西南财经大学天府学院-123123')",img);
        //订单ID，订单时间，购买人，商家，商品ID
        //商品ID，商家用户，名字，描述，单价，数量
        db.execSQL("drop table if exists d_address");//代表的是用户表
        db.execSQL("create table d_address(s_id varchar(30)primary key ," +
                "s_user_id varchar(255) ," +
                "s_user_name varchar(255)," +
                "s_user_address varchar(255)," +
                "s_phone varchar(255))");
        // select * from d_orders
        db.execSQL("insert into d_address values('1','admin','小杨','四川绵阳，西南财经大学天府学院','123456')");
        db.execSQL("insert into d_address values('2','admin','小杨','四川绵阳，西南财经大学天府学院','12345678')");
        db.execSQL("drop table if exists d_order_detail");
        db.execSQL("create table d_order_detail(s_order_business_id varchar(20) ," +
                "s_food_id varchar(20) ," +
                "s_food_name varchar(20)," +
                "s_food_des varchar(200)," +
                "s_food_price varchar(20)," +
                "s_food_img varchar(255)," +
                "s_food_time varchar(200)," +
                "s_food_num varchar(255))");

        String klms[]={klmPath};
        String mlts[]={mltPath};

        db.execSQL("insert into d_order_detail values('1','2','杨枝甘露1','属于你的唯一奶茶1','12.9',?,'2023-12-07','10')",klms);
        db.execSQL("insert into d_order_detail values('1','1','杨枝甘露2','属于你的唯一奶茶2','12.9',?,'2023-12-07','15')",mlts);
        db.execSQL("insert into d_order_detail values('1','1','杨枝甘露3','属于你的唯一奶茶3','12.9',?,'2023-12-07','20')",mlts);

        db.execSQL("insert into d_order_detail values('2','1','杨枝甘露1','属于你的唯一奶茶1','12.9',?,'2023-12-07','11')",mlts);
        db.execSQL("insert into d_order_detail values('2','1','杨枝甘露2','属于你的唯一奶茶2','12.9',?,'2023-12-07','13')",mlts);
        db.execSQL("insert into d_order_detail values('2','1','杨枝甘露3','属于你的唯一奶茶3','12.9',?,'2023-12-07','14')",mlts);

        db.execSQL("drop table if exists d_comments");//如果这表存在则删除  代表的是用户表
        //评价管理，默认的是不可以删除评级的内容
        db.execSQL("create table d_comments(s_comment_id varchar(255)primary key ," +//评论ID
                "s_comment_user_id varchar(255) ," +
                "s_comment_user_name varchar(255)," +
                "s_comment_user_img varchar(255)," +
                "s_comment_contenct varchar(255)," +
                "s_comment_img varchar(255)," +
                "s_comment_time varchar(255)," +
                "s_comment_business_id varchar(255)," +
                "s_comment_score varchar(255))");
        String imgz[]={filePath};
        db.execSQL("insert into d_comments values('1','admin','小杨',?,'这个菜是真的好吃',?,'2023-10-07 10:56','root','5')", img);
        db.execSQL("insert into d_comments values('2','admin','小杨',?,'评论','','2023-10-25 10:56','root','3')",img);

        db.execSQL("PRAGMA foreign_keys = true");
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
            onCreate(sqLiteDatabase);
    }
    private byte[] getImg(int value){
        Resources res  =context.getResources();//获取资源
        Bitmap mlt = BitmapFactory.decodeResource(res,value); // 杨枝甘露
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();  // 将Bitmap转换为字节数组
        mlt.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
        byte[] imageByteArray = byteArrayOutputStream.toByteArray();// 执行插入操作
        return imageByteArray;
    }
}
